/*     */ package com.jcrj.service;
/*     */ 
/*     */ import com.jcrj.dao.MessageDao;
/*     */ import com.jcrj.entity.ODraftMessages;
/*     */ import com.jcrj.entity.OMessageMajor;
/*     */ import com.jcrj.entity.OMessageSet;
/*     */ import com.jcrj.entity.OMessagesReceiver;
/*     */ import com.jcrj.entity.OTextmessages;
/*     */ import com.jcrj.entity.OUser;
/*     */ import com.jcrj.util.SendMessage;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ 
/*     */ public class MessageService
/*     */ {
/*     */   private MessageDao messageDao;
/*     */ 
/*     */   public MessageDao getMessageDao()
/*     */   {
/*  20 */     return this.messageDao;
/*     */   }
/*     */ 
/*     */   public void setMessageDao(MessageDao messageDao) {
/*  24 */     this.messageDao = messageDao;
/*     */   }
/*     */   public OUser getUserById(int id) {
/*  27 */     return (OUser)this.messageDao.getObject(new OUser(), id);
/*     */   }
/*     */ 
/*     */   public String saveMessage(ODraftMessages draftMessage) {
/*  31 */     String hql = "from OMessageMajor o where o.OMessageSet.del='0' and o.userId in (" + draftMessage.getSendId() + ")";
/*  32 */     List messageMajors = this.messageDao.findByHql(hql);
/*  33 */     if (messageMajors.size() == 0)
/*     */     {
/*  35 */       OTextmessages message = new OTextmessages();
/*  36 */       message.setContent(draftMessage.getContent());
/*  37 */       message.setCreateTime(draftMessage.getCreateTime());
/*  38 */       message.setExnumber(draftMessage.getExnumber());
/*  39 */       message.setExreceivers(draftMessage.getExreceivers());
/*  40 */       message.setMessageflag(draftMessage.getMessageflag());
/*  41 */       message.setModulename(draftMessage.getModulename());
/*  42 */       message.setPriority(draftMessage.getPriority());
/*  43 */       message.setReceiverIds(draftMessage.getReceiverIds());
/*  44 */       message.setReceiverNames(draftMessage.getReceiverNames());
/*  45 */       message.setSendId(draftMessage.getSendId());
/*  46 */       message.setSendName(draftMessage.getSendName());
/*  47 */       message.setDel("0");
/*  48 */       this.messageDao.saveMessage(message);
/*  49 */       String[] array = message.getReceiverIds().split(",");
/*  50 */       for (int i = 0; i < array.length; i++) {
/*  51 */         if (!"".equals(array[i])) {
/*  52 */           OMessagesReceiver messageReceiver = new OMessagesReceiver();
/*  53 */           messageReceiver.setOTextmessages(message);
/*  54 */           messageReceiver.setReceiverId(Integer.valueOf(array[i]));
/*  55 */           messageReceiver.setDel("0");
/*  56 */           this.messageDao.saveMessageReceiver(messageReceiver);
/*     */         }
/*     */       }
/*  59 */       return sendSMS(message, message.getSendName());
/*     */     }
/*  61 */     OMessageSet messageSet = ((OMessageMajor)messageMajors.get(0)).getOMessageSet();
/*  62 */     if (messageSet.getMessageNumber().intValue() == 0) {
/*  63 */       return "你的可发短信数为0，请与管理员联系！";
/*     */     }
/*  65 */     messageSet.setMessageNumber(Integer.valueOf(messageSet.getMessageNumber().intValue() - 1));
/*  66 */     this.messageDao.saveMessageSet(messageSet);
/*     */ 
/*  69 */     OTextmessages message = new OTextmessages();
/*  70 */     message.setContent(draftMessage.getContent());
/*  71 */     message.setCreateTime(draftMessage.getCreateTime());
/*  72 */     message.setExnumber(draftMessage.getExnumber());
/*  73 */     message.setExreceivers(draftMessage.getExreceivers());
/*  74 */     message.setMessageflag(draftMessage.getMessageflag());
/*  75 */     message.setModulename(draftMessage.getModulename());
/*  76 */     message.setPriority(draftMessage.getPriority());
/*  77 */     message.setReceiverIds(draftMessage.getReceiverIds());
/*  78 */     message.setReceiverNames(draftMessage.getReceiverNames());
/*  79 */     message.setSendId(draftMessage.getSendId());
/*  80 */     message.setSendName(draftMessage.getSendName());
/*  81 */     message.setDel("0");
/*  82 */     this.messageDao.saveMessage(message);
/*  83 */     String[] array = message.getReceiverIds().split(",");
/*  84 */     for (int i = 0; i < array.length; i++) {
/*  85 */       if (!"".equals(array[i])) {
/*  86 */         OMessagesReceiver messageReceiver = new OMessagesReceiver();
/*  87 */         messageReceiver.setOTextmessages(message);
/*  88 */         messageReceiver.setReceiverId(Integer.valueOf(array[i]));
/*  89 */         messageReceiver.setDel("0");
/*  90 */         this.messageDao.saveMessageReceiver(messageReceiver);
/*     */       }
/*     */     }
/*  93 */     return sendSMS(message, message.getSendName());
/*     */   }
/*     */ 
/*     */   public void saveDraftMessage(ODraftMessages draftMessage) {
/*  97 */     this.messageDao.saveDraftMessage(draftMessage);
/*     */   }
/*     */ 
/*     */   public List<OMessagesReceiver> receivedMessages(OUser user, String content, String startDate, String endDate, int currentPage)
/*     */   {
/* 103 */     String hql = "from OMessagesReceiver o where o.del='0' and o.receiverId=" + user.getId();
/* 104 */     if ((content != null) && (!"".equals(content.trim()))) {
/* 105 */       hql = hql + " and o.OTextmessages.content like '%" + content.trim() + "%'";
/*     */     }
/* 107 */     if ((startDate != null) && (!"".equals(startDate.trim()))) {
/* 108 */       hql = hql + " and o.OTextmessages.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 110 */     if ((endDate != null) && (!"".equals(endDate.trim()))) {
/* 111 */       hql = hql + " and o.OTextmessages.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 113 */     hql = hql + " order by o.id desc";
/* 114 */     return this.messageDao.findByPage(hql, currentPage);
/*     */   }
/*     */   public long receivedMessagesCount(OUser user, String content, String startDate, String endDate, int currentPage) {
/* 117 */     String hql = "select count(id) from OMessagesReceiver o where o.del='0' and o.receiverId=" + user.getId();
/* 118 */     if ((content != null) && (!"".equals(content.trim()))) {
/* 119 */       hql = hql + " and o.OTextmessages.content like '%" + content.trim() + "%'";
/*     */     }
/* 121 */     if ((startDate != null) && (!"".equals(startDate.trim()))) {
/* 122 */       hql = hql + " and o.OTextmessages.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 124 */     if ((endDate != null) && (!"".equals(endDate.trim()))) {
/* 125 */       hql = hql + " and o.OTextmessages.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 127 */     return this.messageDao.countTotalByHql(hql).longValue();
/*     */   }
/*     */ 
/*     */   public void updateMessage(String[] ids, OUser user) {
/* 131 */     if ((ids != null) && (ids.length > 0)) {
/* 132 */       String messageId = "";
/* 133 */       for (int i = 0; i < ids.length; i++) {
/* 134 */         if ("".equals(messageId))
/* 135 */           messageId = messageId + ids[i];
/*     */         else {
/* 137 */           messageId = messageId + "," + ids[i];
/*     */         }
/*     */       }
/* 140 */       String hql = "update OMessagesReceiver o set o.del='1' where o.id in(" + messageId + ") and o.receiverId=" + user.getId();
/* 141 */       this.messageDao.executeHql(hql);
/*     */     }
/*     */   }
/*     */ 
/*     */   public List<ODraftMessages> draftMessages(OUser user, String content, String startDate, String endDate, int currentPage)
/*     */   {
/* 153 */     String hql = "from ODraftMessages o where o.sendId=" + user.getId();
/* 154 */     if ((content != null) && (!"".equals(content.trim()))) {
/* 155 */       hql = hql + " and o.content like '%" + content.trim() + "%'";
/*     */     }
/* 157 */     if ((startDate != null) && (!"".equals(startDate.trim()))) {
/* 158 */       hql = hql + " and o.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 160 */     if ((endDate != null) && (!"".equals(endDate.trim()))) {
/* 161 */       hql = hql + " and o.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 163 */     hql = hql + " order by o.id desc";
/* 164 */     return this.messageDao.findByPage(hql, currentPage);
/*     */   }
/*     */   public long draftMessagesCount(OUser user, String content, String startDate, String endDate, int currentPage) {
/* 167 */     String hql = "select count(id) from ODraftMessages o where o.sendId=" + user.getId();
/* 168 */     if ((content != null) && (!"".equals(content.trim()))) {
/* 169 */       hql = hql + " and o.content like '%" + content.trim() + "%'";
/*     */     }
/* 171 */     if ((startDate != null) && (!"".equals(startDate.trim()))) {
/* 172 */       hql = hql + " and o.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 174 */     if ((endDate != null) && (!"".equals(endDate.trim()))) {
/* 175 */       hql = hql + " and o.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 177 */     return this.messageDao.countTotalByHql(hql).longValue();
/*     */   }
/*     */ 
/*     */   public ODraftMessages getDraftMessage(String messageId) {
/* 181 */     String hql = "from ODraftMessages o where o.id=" + messageId;
/* 182 */     return (ODraftMessages)this.messageDao.findByHql(hql).get(0);
/*     */   }
/*     */ 
/*     */   public void deleteDraftMessage(String[] ids, OUser user) {
/* 186 */     if ((ids != null) && (ids.length > 0)) {
/* 187 */       String messageId = "";
/* 188 */       for (int i = 0; i < ids.length; i++) {
/* 189 */         if ("".equals(messageId))
/* 190 */           messageId = messageId + ids[i];
/*     */         else {
/* 192 */           messageId = messageId + "," + ids[i];
/*     */         }
/*     */       }
/* 195 */       String hql = "delete ODraftMessages o where o.id in(" + messageId + ")";
/* 196 */       this.messageDao.executeHql(hql);
/*     */     }
/*     */   }
/*     */ 
/*     */   public List<OTextmessages> sendeddMessages(OUser user, String content, String startDate, String endDate, int currentPage)
/*     */   {
/* 208 */     String hql = "from OTextmessages o where o.del='0' and o.sendId=" + user.getId();
/* 209 */     if ((content != null) && (!"".equals(content.trim()))) {
/* 210 */       hql = hql + " and o.content like '%" + content.trim() + "%'";
/*     */     }
/* 212 */     if ((startDate != null) && (!"".equals(startDate.trim()))) {
/* 213 */       hql = hql + " and o.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 215 */     if ((endDate != null) && (!"".equals(endDate.trim()))) {
/* 216 */       hql = hql + " and o.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 218 */     hql = hql + " order by o.id desc";
/* 219 */     return this.messageDao.findByPage(hql, currentPage);
/*     */   }
/*     */   public long sendedMessagesCount(OUser user, String content, String startDate, String endDate, int currentPage) {
/* 222 */     String hql = "select count(id) from OTextmessages o where o.del='0' and o.sendId=" + user.getId();
/* 223 */     if ((content != null) && (!"".equals(content.trim()))) {
/* 224 */       hql = hql + " and o.content like '%" + content.trim() + "%'";
/*     */     }
/* 226 */     if ((startDate != null) && (!"".equals(startDate.trim()))) {
/* 227 */       hql = hql + " and o.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 229 */     if ((endDate != null) && (!"".equals(endDate.trim()))) {
/* 230 */       hql = hql + " and o.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 232 */     return this.messageDao.countTotalByHql(hql).longValue();
/*     */   }
/*     */ 
/*     */   public void updateSendedMessage(String[] ids, OUser user) {
/* 236 */     if ((ids != null) && (ids.length > 0)) {
/* 237 */       String messageId = "";
/* 238 */       for (int i = 0; i < ids.length; i++) {
/* 239 */         if ("".equals(messageId))
/* 240 */           messageId = messageId + ids[i];
/*     */         else {
/* 242 */           messageId = messageId + "," + ids[i];
/*     */         }
/*     */       }
/* 245 */       String hql = "update OTextmessages o set o.del='1' where o.id in(" + messageId + ") and o.sendId=" + user.getId();
/* 246 */       this.messageDao.executeHql(hql);
/*     */     }
/*     */   }
/*     */ 
/*     */   public List<OMessagesReceiver> deletedMessages(OUser user, String content, String startDate, String endDate, int currentPage)
/*     */   {
/* 257 */     String hql = "from OMessagesReceiver o where o.del='1' and o.receiverId=" + user.getId();
/* 258 */     if ((content != null) && (!"".equals(content.trim()))) {
/* 259 */       hql = hql + " and o.OTextmessages.content like '%" + content.trim() + "%'";
/*     */     }
/* 261 */     if ((startDate != null) && (!"".equals(startDate.trim()))) {
/* 262 */       hql = hql + " and o.OTextmessages.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 264 */     if ((endDate != null) && (!"".equals(endDate.trim()))) {
/* 265 */       hql = hql + " and o.OTextmessages.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 267 */     hql = hql + " order by o.id desc";
/* 268 */     return this.messageDao.findByPage(hql, currentPage);
/*     */   }
/*     */   public long deletedMessagesCount(OUser user, String content, String startDate, String endDate, int currentPage) {
/* 271 */     String hql = "select count(id) from OMessagesReceiver o where o.del='1'and o.receiverId=" + user.getId();
/* 272 */     if ((content != null) && (!"".equals(content.trim()))) {
/* 273 */       hql = hql + " and o.OTextmessages.content like '%" + content.trim() + "%'";
/*     */     }
/* 275 */     if ((startDate != null) && (!"".equals(startDate.trim()))) {
/* 276 */       hql = hql + " and o.OTextmessages.createTime >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 278 */     if ((endDate != null) && (!"".equals(endDate.trim()))) {
/* 279 */       hql = hql + " and o.OTextmessages.createTime <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')";
/*     */     }
/* 281 */     return this.messageDao.countTotalByHql(hql).longValue();
/*     */   }
/*     */ 
/*     */   public void deleteMessage(String[] ids, OUser user) {
/* 285 */     if ((ids != null) && (ids.length > 0)) {
/* 286 */       String messageId = "";
/* 287 */       for (int i = 0; i < ids.length; i++) {
/* 288 */         if ("".equals(messageId))
/* 289 */           messageId = messageId + ids[i];
/*     */         else {
/* 291 */           messageId = messageId + "," + ids[i];
/*     */         }
/*     */       }
/* 294 */       String hql = "delete OMessagesReceiver o where o.id in(" + messageId + ") and o.receiverId=" + user.getId();
/* 295 */       this.messageDao.executeHql(hql);
/*     */     }
/*     */   }
/*     */ 
/*     */   public List<OMessageSet> messageSet(int currentPage)
/*     */   {
/* 310 */     String hql = "from OMessageSet o where o.del='0' order by o.id desc";
/* 311 */     return this.messageDao.findByPage(hql, currentPage);
/*     */   }
/*     */   public long messageSetCount(int currentPage) {
/* 314 */     String hql = "select count(id) from OMessageSet o where o.del='0'";
/* 315 */     return this.messageDao.countTotalByHql(hql).longValue();
/*     */   }
/*     */   public String saveMessageSet(OMessageSet messageSet, String status) {
/* 318 */     String info = null;
/*     */ 
/* 320 */     String[] ids = messageSet.getUserIds().split(",");
/* 321 */     String[] names = messageSet.getUserNames().split(",");
/* 322 */     List existingIds = new ArrayList();
/* 323 */     List existingNames = new ArrayList();
/* 324 */     if ("a".equals(status)) {
/* 325 */       for (int i = 0; i < ids.length; i++) {
/* 326 */         String hql = "select count(id) from OMessageMajor o where o.OMessageSet.del='0' and o.userId in (" + ids[i] + ")";
/* 327 */         if (this.messageDao.countTotalByHql(hql).longValue() != 0L) {
/* 328 */           existingIds.add(ids[i]);
/* 329 */           existingNames.add(names[i]);
/*     */         }
/*     */       }
/* 332 */       if (existingIds.size() == 0) {
/* 333 */         this.messageDao.saveMessageSet(messageSet);
/* 334 */         for (String id : ids) {
/* 335 */           OMessageMajor messageMajor = new OMessageMajor();
/* 336 */           messageMajor.setOMessageSet(messageSet);
/* 337 */           messageMajor.setUserId(Integer.valueOf(id));
/* 338 */           this.messageDao.saveMessageMajor(messageMajor);
/*     */         }
/* 340 */         info = "保存成功！";
/*     */       }
/*     */       else {
/* 343 */         info = "保存失败！下列用户已设置：";
/* 344 */         for (int i = 0; i < existingNames.size(); i++) {
/* 345 */           info = info + (String)existingNames.get(i) + ",";
/*     */         }
/*     */       }
/*     */     }
/* 349 */     if ("u".equals(status)) {
/* 350 */       String hql = "update OMessageSet o set o.del='1' where o.id in(" + messageSet.getId() + ")";
/* 351 */       this.messageDao.executeHql(hql);
/*     */ 
/* 353 */       for (int i = 0; i < ids.length; i++) {
/* 354 */         hql = "select count(id) from OMessageMajor o where o.OMessageSet.del='0' and o.userId in (" + ids[i] + ")";
/* 355 */         if (this.messageDao.countTotalByHql(hql).longValue() != 0L) {
/* 356 */           existingIds.add(ids[i]);
/* 357 */           existingNames.add(names[i]);
/*     */         }
/*     */       }
/* 360 */       if (existingIds.size() == 0) {
/* 361 */         this.messageDao.saveMessageSet(messageSet);
/* 362 */         for (String id : ids) {
/* 363 */           OMessageMajor messageMajor = new OMessageMajor();
/* 364 */           messageMajor.setOMessageSet(messageSet);
/* 365 */           messageMajor.setUserId(Integer.valueOf(id));
/* 366 */           this.messageDao.saveMessageMajor(messageMajor);
/*     */         }
/* 368 */         info = "保存成功！";
/*     */       }
/*     */       else {
/* 371 */         hql = "update OMessageSet o set o.del='0' where o.id in(" + messageSet.getId() + ")";
/* 372 */         this.messageDao.executeHql(hql);
/* 373 */         info = "保存失败！下列用户已设置：";
/* 374 */         for (int i = 0; i < existingNames.size(); i++) {
/* 375 */           info = info + (String)existingNames.get(i) + ",";
/*     */         }
/*     */       }
/*     */     }
/* 379 */     return info;
/*     */   }
/*     */   public OMessageSet getMessageSet(OMessageSet messageSet) {
/* 382 */     return (OMessageSet)this.messageDao.getObject(messageSet, messageSet.getId().intValue());
/*     */   }
/*     */   public void deleteMessageSet(String[] ids) {
/* 385 */     if ((ids != null) && (ids.length > 0)) {
/* 386 */       String messageSetId = "";
/* 387 */       for (int i = 0; i < ids.length; i++) {
/* 388 */         if ("".equals(messageSetId))
/* 389 */           messageSetId = messageSetId + ids[i];
/*     */         else {
/* 391 */           messageSetId = messageSetId + "," + ids[i];
/*     */         }
/*     */       }
/* 394 */       String hql = "update OMessageSet o set o.del='1' where o.id in(" + messageSetId + ")";
/* 395 */       this.messageDao.executeHql(hql);
/*     */     }
/*     */   }
/*     */ 
/*     */   public String sendSMS(OTextmessages message, String userName)
/*     */   {
/* 414 */     int flag = 0;
/* 415 */     String info = "下列号码未发送成功：";
/* 416 */     List mobilePhones = new ArrayList();
/* 417 */     String[] phones = message.getExreceivers().split(",");
/* 418 */     for (String phone : phones) {
/* 419 */       if (!"".equals(phone)) {
/* 420 */         mobilePhones.add(phone);
/*     */       }
/*     */     }
/* 423 */     String[] names = message.getReceiverNames().split(",");
/* 424 */     for (String name : names) {
/* 425 */       if (!"".equals(name)) {
/* 426 */         int start = name.indexOf("(");
/* 427 */         int end = name.indexOf(")");
/* 428 */         if ((start != -1) && (end != -1)) {
/* 429 */           mobilePhones.add(name.substring(start + 1, end));
/*     */         } else {
/* 431 */           flag++;
/* 432 */           info = info + name + "用户无手机号码未发送成功！";
/*     */         }
/*     */       }
/*     */     }
/* 436 */     for (int i = 0; i < mobilePhones.size(); i++) {
/* 437 */       SendMessage sendMessage = new SendMessage();
/* 438 */       boolean success = sendMessage.sendSMS(message.getContent() + "（来自：" + userName + "）", (String)mobilePhones.get(i), message.getPriority(), message.getMessageflag(), message.getModulename(), message.getExnumber());
/* 439 */       if (!success) {
/* 440 */         flag++;
/* 441 */         info = info + (String)mobilePhones.get(i) + ",";
/*     */       }
/*     */     }
/* 444 */     if (flag == 0) {
/* 445 */       info = "全部号码发送成功";
/*     */     }
/* 447 */     return info;
/*     */   }
/*     */ 
/*     */   public void send(String phone, String content)
/*     */   {
/* 455 */     SendMessage sendMessage = new SendMessage();
/* 456 */     sendMessage.sendSMS(content.trim(), phone.trim(), "1", "1", "1", "1");
/*     */   }
/*     */ }

/* Location:           /Volumes/work/oa/WEB-INF/classes/
 * Qualified Name:     com.jcrj.service.MessageService
 * JD-Core Version:    0.6.0
 */